<?php

/**
 * 模型对应数据库表 "us_city".
 *
 * 以下是可用表us_city的栏位:
 * @property string $id
 * @property string $name
 * @property integer $parentid
 * @property integer $status
 * @property integer $sort
 */
class UsCity extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return UsCity the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_country_city';
    }

    /**
     * 获取顶级目录
     */
    public function getParents() {
        $data = array();
        $criteria = new CDbCriteria;
        $criteria->compare('parentid', 0);
        $criteria->order = 'us_sort ASC';
        $find = $this->findAll($criteria);
        foreach ($find as $v) {
            $data[$v->id] = $v->name;
        }
        return $data;
    }

    /**
     * 状态显示 
     */
    public $state_array = array(
        '0' => '显示',
        '1' => '隐藏',
    );

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('name', 'required'),
            array('parentid, status, us_sort', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, parentid, status, us_sort', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'name' => '地区名称',
            'parentid' => '上级地区',
            'status' => '状态',
            'us_sort' => '排序',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('parentid', $this->parentid);
        $criteria->compare('status', $this->status);
        $criteria->compare('us_sort', $this->us_sort);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}